package com.interviewbar.system.controller;

import com.interviewbar.common.vo.Result;
import com.interviewbar.system.entity.DeviceTestOne;
import com.interviewbar.system.service.DeviceTestOneService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;


/**
 * @author FangGL
 */
@RestController
@RequestMapping("/deviceTestOne")
public class DeviceTestOneController {

    private final DeviceTestOneService deviceTestOneService;

    public DeviceTestOneController(DeviceTestOneService deviceTestOneService) {
        this.deviceTestOneService = deviceTestOneService;
    }

    /**
     * 查询表中所有数据
     * @return
     */
    @GetMapping("/list")
    public Result list() {
        List<DeviceTestOne> list = deviceTestOneService.list();
        return Result.success(list);
    }



    /**
     * 查询表中所有数据
     * @return
     */
    @GetMapping("/getPageAll")
    public Result getPageAll() {
        List<DeviceTestOne> list = new ArrayList<>();

        int limit = 50000;
        long count = deviceTestOneService.count();
        //循环次数
        long cycles = count / limit+1;

        for (int i = 0; i < cycles; i++) {
            long startIdx = i * limit;
            long endIdx = (i + 1) * limit;
            if (endIdx > count){
                endIdx = count;
            }
            List<DeviceTestOne> list1 = deviceTestOneService.getPageAll(startIdx, endIdx);
            list.addAll(list1);
        }
        return Result.success(list);
    }


    @GetMapping("/multithreading")
    public Result multithreading() {
        List<DeviceTestOne> list = new ArrayList<>();

        int limit = 50000;
        long count = deviceTestOneService.count();
        //循环次数
        long cycles = count / limit+1;


        CountDownLatch latch = new CountDownLatch(Math.toIntExact(cycles));
        ExecutorService executorService = Executors.newFixedThreadPool(Math.toIntExact(cycles));

        for (int i = 0; i < cycles; i++) {
            final int currentIndex = i;
            executorService.submit(() -> {
                int startIdx = currentIndex * limit;
                int endIdx = (currentIndex + 1) * limit;
                if (endIdx > count) {
                    endIdx = Math.toIntExact(count);
                }
                List<DeviceTestOne> listTemp = deviceTestOneService.getPageAll(startIdx, endIdx);
                list.addAll(listTemp);
                latch.countDown(); // 通知完成
            });
        }

        try {
            // 等待所有任务完成
            latch.await();
            // 没有异常发生，开始关闭线程池
            executorService.shutdown();
            // 等待线程池关闭完成，或者达到超时时间
            if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
                // 如果超时，强制关闭线程池
                executorService.shutdownNow();
            }
        } catch (InterruptedException e) {
            // 发生中断异常，重新设置中断状态
            Thread.currentThread().interrupt();
            // 尝试关闭线程池
            executorService.shutdownNow();
        }
        return Result.success(list);
    }
}
